﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Library
{
    public partial class AuthorViewForm : Form
    {
        public Author author = new Author();
        bool add = false;
        LibraryEntities1 ins = new LibraryEntities1();
        public static ObjectSet<Author> authors;
        public AuthorViewForm(Author author, bool add)
        {
            InitializeComponent();
            this.author = author;
            this.add = add;
            authors = ins.Author;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AuthorViewForm_Load(object sender, EventArgs e)
        {
            if (author != null)
            {
                TBName.Text = author.Name;
                TBSurname.Text = author.Surname;
                TBPatronymic.Text = author.Patronymic;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (author == null)
                author = new Author();
            else
                author = authors.Where(x => x.idAuthor.Equals(author.idAuthor)).FirstOrDefault();

            author.Name = TBName.Text;
            author.Surname = TBSurname.Text;
            author.Patronymic = TBPatronymic.Text;

            if (add)
            {
                ins.AddToAuthor(author);
            }
            ins.SaveChanges();

            this.Close();
        }
    }
}
